package com.example.demo.LeetCode.list;

import java.util.ArrayList;
import java.util.List;

public class ReversPrintList {

    public static int[] reversePrint(ListNode head) {
      List<Integer> list = new ArrayList<>();
      ListNode p = head;
      while(p!=null){
          list.add(p.val);
          p = p.next;
        }
      int[] intArr = new int[list.size()];
      int j = 0;
      for (int i = list.size()-1 ; i>=0 ; i--){
          intArr[j] = list.get(i);
          j++;
      }
      return intArr;
    }


    public static int[] reversePrint1(ListNode head) {
        int size = 0;
        ListNode p = head;
        while(p!= null){
            size ++;
            p = p.next;
        }

        int[] intArr = new int[size];
        ListNode q = head;
        int nSize = size-1;
        while (q!=null){
            intArr[nSize] = q.val;
            nSize--;
            q = q.next;
        }
        return intArr;
    }


    private static  ListNode createListNode1(){
        ListNode head1 = new ListNode(1);
        ListNode next1 = new ListNode(4);
        ListNode next2 = new ListNode(5);
        head1.next = next1;
        next1.next = next2;
        return head1;
    }

    public static void main(String[] args) {
        ListNode head = createListNode1();
        int[] arr = reversePrint1(head);
        for (int i : arr) {
            System.out.println(i);
        }
    }
}
